package com.beidasoft.web.util;

import com.beidasoft.web.model.system.DictionaryList;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Ronin on 2015/12/14.
 */
public class CommonUtil {
    /**
     * 获取系统当前时间,并按指定格式输出
     *
     * @return
     */
    public static String getCommonDate() {
        SimpleDateFormat str = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = str.format(new Date());
        return nowDate;
    }

    /**
     * 查询当前年份
     *
     * @return
     */
    public static String getCurYear() {
        SimpleDateFormat str = new SimpleDateFormat("yyyy");
        String nowDate = str.format(new Date());
        return nowDate;
    }

    public static String getCommonDateSecond() {
        SimpleDateFormat str = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowDate = str.format(new Date());
        return nowDate;
    }

    public static List<DictionaryList> getBeforeYear() {
        List<DictionaryList> yearList = new ArrayList<>();
        Integer year = Calendar.getInstance().get(Calendar.YEAR);
        for (int i = 0; i <= year - 2014; i++) {
            DictionaryList dictionary = new DictionaryList();
            Integer tmp = year - i;
            dictionary.setId(tmp.toString());
            yearList.add(dictionary);
        }
        return yearList;
    }

    public static String fillZero(String str, int length) throws Exception {
        str = str.trim();
        if (str.length() > length) {
            throw new RuntimeException("生成法条内容编号异常,某字段值'" + str + "'超出规定长度" + length);
        }
        while (str.length() < length) {
            str = new StringBuffer(str).insert(0, '0').toString();
        }
        return str;
    }

    public static String generateLawDetailCode(Integer lawNum, Integer strip, Integer fund, Integer item) throws Exception {
        String lawNumStr = Integer.toString(lawNum);
        if (lawNumStr.length() != 5) {
            throw new RuntimeException("原法律编号" + lawNumStr + "长度不符合规定");
        }
        StringBuffer sb = new StringBuffer(lawNumStr);
        if (strip == null) {
            sb.append(fillZero("", 3));
        } else {
            sb.append(fillZero(Integer.toString(strip), 3));
        }
        if (fund == null) {
            sb.append(fillZero("", 2));
        } else {
            sb.append(fillZero(Integer.toString(fund), 2));
        }
        if (item == null) {
            sb.append(fillZero("", 2));
        } else {
            sb.append(fillZero(Integer.toString(item), 2));
        }
        return sb.toString();
    }

    /**
     * 字符串转时间
     *
     * @param dateStr 时间格式字符串
     * @param pattern 转成的时间格式
     * @return 返回时间类型，字符串为空，则返回空
     * @throws Exception
     */
    public static Date formatDate(String dateStr, String pattern) throws Exception {
        Date date = null;
        //注意：SimpleDateFormat构造函数的样式与dateStr的样式必须相符
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        //必须捕获异常
        try {
            if (dateStr != null && !"".equals(dateStr)) {
                date = simpleDateFormat.parse(dateStr);
            } else {
                date = null;
            }
        } catch (ParseException px) {
            px.printStackTrace();
        }
        return date;
    }

    /**
     * 时间类型转字符串
     *
     * @param date    时间
     * @param pattern 要转的时间格式
     * @return 返回时间类型字符串
     * @throws Exception
     */
    public static String formatString(Date date, String pattern) throws Exception {
        String dateStr = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            if (date != null) {
                dateStr = simpleDateFormat.format(date);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * 比较两个时间字符串对应时间先后
     *
     * @param date1   时间1
     * @param date2   时间2
     * @param pattren 时间格式
     * @return 返回1，date1 大于date2 ，返回-1 date1 小于 date2，返回0 则两者相等
     * @throws Exception
     */
    public static int compareDate(String date1, String date2, String pattren) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat(pattren);
        try {
            //注意：SimpleDateFormat构造函数的样式与date1，date2的样式必须相符
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                //System.out.println("dt1大于dt2");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                //System.out.println("dt1小于dt2");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * @return int
     * @Author songff
     * @MethodName differentDaysByMillisecond
     * @Description //TODO 获取2个时间相差的天数
     * @Date 17:58 2019/12/27
     * @Param [date1, date2]
     **/
    public static int getDifferDaysByMillisecond(Date date1, Date date2) {
        int days = 0;
        if (date1 != null && date2 != null) {
            if (date1.getTime() < date2.getTime()) {
                //System.out.println("date1小于date2");
                days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
            } else {
                //System.out.println("date1大于date2");
                days = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
            }
        }
        return days;
    }

    /**
     * @return int
     * @Author songff
     * @MethodName randomNum
     * @Description //TODO 随机生成1~5的整数
     * @Date 16:26 2019/12/4
     * @Param []
     **/
    public static int randomNum() {
        int num = (int) (Math.random() * 10);
        return num;
    }

    /**
     * @param testStr
     * @return boolean
     * @Author 曹进宇
     * @MethodName isStrAbleToParseInt
     * @Description //TODO 校验String参数是否可以转换成Integer
     * @Date 17:28 2020/4/1
     **/
    public static boolean isStrAbleToParseInt(String testStr) {
        try {
            Integer.parseInt(testStr);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
